from pms.common.views import PostFormula
from pms.common.utils import flatten_view
from project.utils.create import create_project
from project.utils.profile import project_view


class API(PostFormula):

    require_permissions = [4]

    def handle(self, req):
        name = req['name']
        argv = flatten_view(dict(req), {
            'leader': ['leader', 'id'],
            'top_designer': ['top_designer', 'user', 'id'],
            'current_version': 'current_version',
            'detail': 'detail'
        })
        date = req.get('start_time', None)
        project = create_project(name, self.user, argv.get('leader', None), argv.get('top_designer', None), date,
                                 argv.get('current_version', ''), argv.get('detail', ''))
        return self.success_respond(project_view(project))
